package cn.book.bus.service.impl;

import cn.book.bus.deepseek.request.DeepSeekRequest;
import cn.book.bus.deepseek.response.DeepSeekResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
import java.util.Collections;

@Service
public class DeepSeekService {

    private final WebClient webClient;

    public DeepSeekService(@Value("${deepseek.api.url}") String apiUrl,
                           @Value("${deepseek.api.key}") String apiKey) {
        this.webClient = WebClient.builder()
                .baseUrl(apiUrl)
                .defaultHeader("Authorization", "Bearer " + apiKey)
                .build();
    }

    public Mono<String> callDeepSeek(String userMessage) {
        DeepSeekRequest request = new DeepSeekRequest();
        request.setModel("deepseek-chat");
        request.setMessages(Collections.singletonList(new DeepSeekRequest.Message("user", userMessage)));

        return webClient.post()
                .contentType(MediaType.APPLICATION_JSON)
                .bodyValue(request)
                .retrieve()
                .bodyToMono(DeepSeekResponse.class)
                .map(response -> response.getChoices().get(0).getMessage().getContent());
    }
}
